package com.songcha.sakura.ui.view.refresh

import android.content.Context
import android.graphics.drawable.BitmapDrawable
import android.util.AttributeSet
import android.view.View
import android.widget.ImageView
import androidx.annotation.ColorInt
import androidx.annotation.NonNull
import com.bumptech.glide.Glide

import com.scwang.smart.refresh.layout.api.RefreshHeader
import com.scwang.smart.refresh.layout.api.RefreshKernel
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.constant.RefreshState
import com.scwang.smart.refresh.layout.simple.SimpleComponent
import com.songcha.library_base.R

class CustomRefreshHeader
@JvmOverloads
constructor(
    context: Context,
    attrs: AttributeSet?=null,
    defStyle: Int = 0,
) : SimpleComponent(context,attrs,defStyle),RefreshHeader {
    //private var mTitleText:TextView?=null
    private var mIv:ImageView?=null
    private var mSetPrimaryColor=false
    private var mBackgroundColor = 0
    private var mRefreshKernel: RefreshKernel? = null
    private val ANIM_TIME=500

    init {
        initView(context,attrs)

    }

    private fun initView(context: Context, attrs: AttributeSet?) {
        val rootView=View.inflate(context,com.songcha.sakura.R.layout.refresh_header_custom,this)
        mIv=rootView.findViewById(com.songcha.sakura.R.id.iv)

        Glide.with(context).asGif().load(com.songcha.sakura.R.drawable.refresh_prepared).into(mIv!!)


    }

    private fun setPrimaryColor(@ColorInt primaryColor: Int) {
        mSetPrimaryColor = true
        mBackgroundColor = primaryColor
        if (mRefreshKernel != null) {
            mRefreshKernel!!.requestDrawBackgroundFor(this, primaryColor)
        }
    }

    //</editor-fold>
    override fun onInitialized(@NonNull kernel: RefreshKernel, height: Int, maxDragHeight: Int) {
        mRefreshKernel = kernel
        mRefreshKernel!!.requestDrawBackgroundFor(this, mBackgroundColor)
    }

    override fun setPrimaryColors(@ColorInt vararg colors: Int) {
        if (colors.size > 0) {
            val thisView: View = this
            if (thisView.background !is BitmapDrawable && !mSetPrimaryColor) {
                setPrimaryColor(colors[0])
                mSetPrimaryColor = false
            }

        }
    }

    override fun onFinish(refreshLayout: RefreshLayout, success: Boolean): Int {
        if(success){
            try{
                mIv?.let {
                    Glide.with(context).asGif().load(com.songcha.sakura.R.drawable.refresh_success).into(it)
                }
            }catch (e:java.lang.Exception){
                e.printStackTrace()
            }

            //mTitleText?.text="Refresh success"
        }else{
            try{
                mIv?.let {
                    Glide.with(context).asGif().load(com.songcha.sakura.R.drawable.refresh_failed).into(it)
                }
            }catch (e:Exception){
                e.printStackTrace()
            }

            //mTitleText?.text="Refresh Failed"
        }
        return ANIM_TIME
        //return super.onFinish(refreshLayout, success)
    }

    override fun onStateChanged(
        refreshLayout: RefreshLayout,
        oldState: RefreshState,
        newState: RefreshState
    ) {
        super.onStateChanged(refreshLayout, oldState, newState)
        when(newState){
            RefreshState.None->{
                mIv?.let {
                    //maybe activity destoryed
                    try{
                        Glide.with(context).load(com.songcha.sakura.R.drawable.refresh_prepared).into(it)
                    }catch (e:java.lang.Exception){
                        e.printStackTrace()
                    }
                }
            }
            RefreshState.Refreshing->{
                try{
                    mIv?.let {
                        Glide.with(context).asGif().load(com.songcha.sakura.R.drawable.refresh_loading2).into(it)
                    }
                }catch (e:java.lang.Exception){
                    e.printStackTrace()
                }
                //mTitleText?.text="Refreshing"
            }
            RefreshState.RefreshReleased->{
                //mTitleText?.text="RefreshReleased"
                try{
                    mIv?.let {
                        Glide.with(context).asGif().load(com.songcha.sakura.R.drawable.refresh_loading2).into(it)
                    }
                }catch (e:java.lang.Exception){
                    e.printStackTrace()
                }
            }
            RefreshState.ReleaseToRefresh->{
                try{
                    mIv?.let {
                        Glide.with(context).load(com.songcha.sakura.R.drawable.refresh_prepared).into(it)
                    }
                }catch (e:java.lang.Exception){
                    e.printStackTrace()
                }
                //mTitleText?.text="ReleaseToRefresh"
            }
            RefreshState.ReleaseToTwoLevel->{
               // mTitleText?.text="ReleaseToTwoLevel"
            }
            RefreshState.RefreshFinish->{
                //mTitleText?.text="RefreshFinish"
            }
            RefreshState.Loading->{
                //mTitleText?.text="Loading"
            }

            else -> {}
        }

    }

}